using System;
using System.Collections.Generic;
using System.IO;
using System.Web;
using System.Web.UI.WebControls;
namespace HKB.Common
{
	public class UploadFile
	{
		private HttpPostedFile postedFile = null;
		private string savePath = "";
		private double fileLength = 0.0;
		private int tempposition;
		private string tempfilelength;
		private double maxlength = 1048576.0;
		private string extension = ".rar,.zip,.txt,.pdf,.doc,.xls,.ppt,.swf,.png,.jpg,.gif,.iso";
		private string filename = "";
		public HttpPostedFile PostedFile
		{
			get
			{
				return this.postedFile;
			}
			set
			{
				this.postedFile = value;
			}
		}
		public string SavePath
		{
			get
			{
				string result;
				if (this.savePath != "")
				{
					result = HttpContext.Current.Server.MapPath(this.savePath);
				}
				else
				{
					result = HttpContext.Current.Server.MapPath("~/Upload");
				}
				return result;
			}
			set
			{
				this.savePath = value;
			}
		}
		public double FileLength
		{
			get
			{
				return this.fileLength;
			}
			set
			{
				this.fileLength = value;
			}
		}
		public string FileLengthstr
		{
			get
			{
				this.tempfilelength = this.FileLength.ToString();
				this.tempposition = this.tempfilelength.IndexOf('.');
				string result;
				if (this.tempposition == -1)
				{
					result = this.tempfilelength + ".00 kb";
				}
				else
				{
					result = this.tempfilelength.Substring(0, this.tempposition + 3) + " kb";
				}
				return result;
			}
		}
		public double MaxLength
		{
			get
			{
				return this.maxlength;
			}
			set
			{
				this.maxlength = value;
			}
		}
		public string Extension
		{
			get
			{
				return this.extension;
			}
			set
			{
				this.extension = value;
			}
		}
		public string FileName
		{
			get
			{
				return this.filename;
			}
			set
			{
				this.filename = value;
			}
		}
		public UploadFile(string dirpath)
		{
			this.SavePath = dirpath;
		}
		public string DoUploadFile(FileUpload FileUpload1, bool isrename, params string[] ReFileName)
		{
			string result = "";
			if (FileUpload1.HasFile)
			{
				this.PostedFile = FileUpload1.PostedFile;
				try
				{
					this.filename = System.IO.Path.GetFileName(this.PostedFile.FileName);
					System.Random random = new System.Random();
					string text = System.IO.Path.GetExtension(this.PostedFile.FileName).ToLower();
					if (isrename)
					{
						if (ReFileName.Length > 0)
						{
							this.filename = ReFileName[0] + text;
						}
						else
						{
							this.filename = System.DateTime.Now.ToString("yyyyMMddHHmm") + random.Next(1000, 9999).ToString() + text;
						}
					}
					this.FileLength = (double)this.PostedFile.ContentLength / 1024.0;
					string[] array = this.Extension.Split(new char[]
					{
						','
					});
					if (array.Length == 0)
					{
						result = "没有限制上传文件的类型!";
					}
					else
					{
						if (((System.Collections.Generic.ICollection<string>)array).Contains(text))
						{
							if (this.FileLength > this.MaxLength)
							{
								result = "上传文件限制大小:" + this.MaxLength + "kb！";
							}
							else
							{
								string text2 = this.SavePath + "\\" + this.filename;
								if (!System.IO.Directory.Exists(this.SavePath))
								{
									System.IO.Directory.CreateDirectory(this.SavePath);
								}
								this.PostedFile.SaveAs(text2);
							}
						}
						else
						{
							result = "只允许上传" + this.Extension + " 文件!";
						}
					}
				}
				catch (System.Exception ex)
				{
					result = ex.Message;
				}
			}
			else
			{
				result = "no file posted!";
			}
			return result;
		}
		public string DoUploadFile(HttpPostedFile postedFile1, bool isrename, params string[] ReFileName)
		{
			string result = "";
			if (postedFile1 != null)
			{
				this.PostedFile = postedFile1;
				try
				{
					this.filename = System.IO.Path.GetFileName(this.PostedFile.FileName);
					System.Random random = new System.Random();
					string text = System.IO.Path.GetExtension(this.PostedFile.FileName).ToLower();
					if (isrename)
					{
						if (ReFileName.Length > 0)
						{
							this.filename = ReFileName[0] + text;
						}
						else
						{
							this.filename = System.DateTime.Now.ToString("yyyyMMddHHmm") + random.Next(1000, 9999).ToString() + text;
						}
					}
					this.FileLength = (double)this.PostedFile.ContentLength / 1024.0;
					string[] array = this.Extension.Split(new char[]
					{
						','
					});
					if (array.Length == 0)
					{
						result = "没有限制上传文件的类型!";
					}
					else
					{
						if (((System.Collections.Generic.ICollection<string>)array).Contains(text))
						{
							if (this.FileLength > this.MaxLength)
							{
								result = "上传文件限制大小:" + this.MaxLength + "kb！";
							}
							else
							{
								string text2 = this.SavePath + "\\" + this.filename;
								if (!System.IO.Directory.Exists(this.SavePath))
								{
									System.IO.Directory.CreateDirectory(this.SavePath);
								}
								this.PostedFile.SaveAs(text2);
							}
						}
						else
						{
							result = "只允许上传" + this.Extension + " 文件!";
						}
					}
				}
				catch (System.Exception ex)
				{
					result = ex.Message;
				}
			}
			else
			{
				result = "no file posted!";
			}
			return result;
		}
	}
}
